package com.wonderful.map

import android.graphics.PointF
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.wonderful.image.ImageSource
import com.wonderful.map.bean.CirclesBean
import com.wonderful.map.bean.GridPositionBean
import com.wonderful.map.bean.PointBean
import com.wonderful.map.bean.VirtualObstacleBean
import com.wonderful.map.constant.MapConstant
import com.wonderful.map.view.PointView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    private val TAG = "SmartMapView"

    private var isLoadOtherImg = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //initScaleView()

        initMapView()
    }

    private fun initMapView() {
        val mapView = findViewById<MapView>(R.id.map_view)
        mapView.apply {
            setImage(R.drawable.large_image)
            setMapListener(object : MapView.MapViewListener {
                override fun onLoadStart() {
                    TODO("Not yet implemented")
                }

                override fun onLoadFinish() {
                    Toast.makeText(context, "图片加载完毕", Toast.LENGTH_SHORT).show()

                    // 功能点
                    setShowPointsView(true)
                    setPointsData(
                        mutableListOf(
                            PointBean("测试点1", "1", gridX = 100f, gridY = 100f, type = 0),
                            PointBean("测试点2", "2", gridX = 200f, gridY = 200f, type = 1),
                            PointBean("测试点3", "3", gridX = 400f, gridY = 400f, type = 0),
                            PointBean("测试点4", "4", gridX = 800f, gridY = 800f, type = 1),
                            PointBean("测试点5", "5", gridX = 1000f, gridY = 1000f, type = 1),
                        )
                    )

                    // 特征路径
                    setShowFeaturePathView(false)
                    setFeaturePathData(
                        mutableListOf(
                            GridPositionBean(500f, 500f),
                            GridPositionBean(600f, 600f),
                            GridPositionBean(700f, 500f),
                            GridPositionBean(800f, 600f),
                            GridPositionBean(900f, 500f),
                            GridPositionBean(1000f, 600f),
                            GridPositionBean(1000f, 1200f),
                            GridPositionBean(900f, 1100f),
                            GridPositionBean(800f, 1200f),
                            GridPositionBean(700f, 1100f),
                            GridPositionBean(600f, 1200f),
                            GridPositionBean(500f, 1100f),
                        )
                    )

                    // 清洁区域
                    setShowVirtualCleanView(true)
                    setVirtualCleanData(createVirtualBean())
                    getVirtualCleanView().apply {
                        setCanClick(true)
                        setClickListener {
                        }
                    }
                    setPlanPathData(
                        mutableListOf(
                            mutableListOf(
                                GridPositionBean(500f, 500f),
                                GridPositionBean(600f, 600f),
                                GridPositionBean(700f, 500f),
                                GridPositionBean(800f, 600f),
                                GridPositionBean(900f, 500f),
                                GridPositionBean(1000f, 600f),
                                GridPositionBean(1000f, 1200f),
                                GridPositionBean(900f, 1100f),
                                GridPositionBean(800f, 1200f),
                                GridPositionBean(700f, 1100f),
                                GridPositionBean(600f, 1200f),
                                GridPositionBean(500f, 1100f),
                            ),
                            mutableListOf(
                                GridPositionBean(1500f, 1500f),
                                GridPositionBean(1600f, 1600f),
                                GridPositionBean(1700f, 1500f),
                                GridPositionBean(1800f, 1600f),
                                GridPositionBean(1900f, 1500f),
                                GridPositionBean(1000f, 1600f),
                                GridPositionBean(1000f, 1200f),
                                GridPositionBean(1900f, 1800f),
                                GridPositionBean(1800f, 1900f),
                                GridPositionBean(1700f, 1800f),
                                GridPositionBean(1600f, 1900f),
                                GridPositionBean(1500f, 1800f),
                            )
                        )
                    )
                }

                override fun onPointClick(pointView: PointView) {
                    Toast.makeText(context, "点击：${pointView.getPointName()}", Toast.LENGTH_SHORT)
                        .show()
                }

            })
            //setShowLocation(false)
            setLocation(PointF(500f, 500f), 90f)
        }

        CoroutineScope(Dispatchers.Main).launch {
            delay(15 * 1000)
            isLoadOtherImg = true
            val imgState = mapView.getMapView().state
            if (imgState != null) {
                Log.e("TAG", "center=${imgState.center} scale=${imgState.scale} ori=${imgState.orientation}")
            } else {
                Log.e("TAG", "imgState is null")
            }
            val source = ImageSource.resource(R.drawable.luffy02).dimensions(8000, 8000)
            val previewSource = ImageSource.resource(R.drawable.luffy02)
            //mapView.setImage(R.drawable.sanmartino, previewSource, state = imgState)
            mapView.setImage(source, previewSource, state = imgState)
        }
    }

    private fun initScaleView() {
        Log.e(TAG, "onStart")
        val startTime = System.currentTimeMillis()
        var endTime: Long
//        val scaleImageView = findViewById<PinView>(R.id.scale_img)
//        scaleImageView.apply {
//            maxScale = 3f
//            setMinimumDpi(320)
//            setDoubleTapZoomStyle(ScaleImageView.ZOOM_FOCUS_CENTER)
//            setPanLimit(ScaleImageView.PAN_LIMIT_CENTER)
//            setOnImageEventListener(object : DefaultOnImageEventListener() {
//                override fun onReady() {
//                    Log.e(TAG, "onReady")
//                    endTime = System.currentTimeMillis()
//                    Log.e(TAG, "start=$startTime end=$endTime, loadTime=${endTime - startTime}")
//                }
//
//                override fun onImageLoaded() {
//                    Log.e(TAG, "onImageLoaded")
//                }
//
//                override fun onImageLoadError(e: Exception?) {
//                    super.onImageLoadError(e)
//                    Log.e(TAG, "onImageLoadError")
//                }
//
//                override fun onTileLoadError(e: Exception?) {
//                    super.onTileLoadError(e)
//                    Log.e(TAG, "onTileLoadError")
//                }
//            })
//            setPinImage(R.drawable.luffy02)
//        }
//        scaleImageView.setImage(ImageSource.resource(R.drawable.large_image))
    }

    private fun createVirtualBean() = VirtualObstacleBean().apply {
        obstaclesType = MapConstant.EditType.VIRTUAL_WALL
        obstacles.apply {
            polygons.add(
                mutableListOf(
                    GridPositionBean(1700f, 1800f),
                    GridPositionBean(2600f, 2900f),
                    GridPositionBean(1500f, 1800f)
                )
            )
            polylines.add(
                mutableListOf(
                    GridPositionBean(2700f, 2800f),
                    GridPositionBean(2600f, 2900f),
                    GridPositionBean(3500f, 3800f)
                )
            )
            circles.add(CirclesBean(GridPositionBean(3000f, 3000f), 10f))
            circles.add(CirclesBean(GridPositionBean(3200f, 3200f), 50f))
        }
    }
}